package com.lms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.stereotype.Service;

import com.lms.domain.NewTraining;
import com.lms.domain.User;
import com.lms.repository.NewTrainingRepository;

@Service
public class NewTrainingService {
	
	
	
	@Autowired
	private NewTrainingRepository newTrainingRepository;
	
	
	public NewTraining createTraining(NewTraining newTraining) {
		System.out.println("\n\n\n\n\n\n adsasdasdasdasdasd \n\n\n\n\n");
		NewTraining existingTraining = newTrainingRepository.findByTrainingId(newTraining.getTrainingId());
		System.out.println("\n\n\n\n\n 111111122222211111122222222 \n\n\n\n\n\n");
		if (existingTraining != null) {
			throw new RuntimeException("Record already exists!");
		}
		
		return newTrainingRepository.save(newTraining);
	}

	public NewTraining readTraining(NewTraining project) {
		return project;
	}
	
	public List<NewTraining> readAll1() {
		List<NewTraining> projects = new ArrayList<NewTraining>();
		
		EndResult<NewTraining> results = newTrainingRepository.findAll();
		for (NewTraining rs: results) {
			projects.add(rs);
		}
		
		return projects;
	}
	
	public NewTraining updateTraining(NewTraining training) {
		NewTraining existingTraining = newTrainingRepository.findByTrainingId(training.getTrainingId());
		
		if (existingTraining == null) {
			return null;
		}
		
		existingTraining.setTrainingName(training.getTrainingName());
		existingTraining.setDescription(training.getDescription());
		
		//trainingRepository.save(existingProject.getRole());
		return newTrainingRepository.save(existingTraining);
	}
	
	public Boolean delete(NewTraining training) {
		NewTraining existingtraining = newTrainingRepository.findByTrainingId(training.getTrainingId());
		
		if (existingtraining == null) {
			return false;
		}
		
		newTrainingRepository.delete(existingtraining);
		return true;
	}
}
